package com.sgb.packers.viewmodel;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;

import com.sgb.link.api.CommonNet;
import com.sgb.link.entity.ProjectTakeEntity;
import com.sgb.packers.api.PackersNet;
import com.sgb.packers.entity.SalaryStandardEntity;
import com.sgb.packers.entity.WorkRecordsEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.common.base.TeamViewModel;
import com.swgk.core.interfaces.IView;

import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * @author : younghow
 * @date : 2021/11/18 11:32
 * description:
 */
public class PointWorkEditFragViewModel extends TeamViewModel<IView> {

    public MutableLiveData<List<String>> uploadUrls = new MutableLiveData<>();
    public MutableLiveData<Boolean> uploadSuccess = new MutableLiveData<>();
    public MutableLiveData<WorkRecordsEntity> pointDetail = new MutableLiveData<>();
    public MutableLiveData<Boolean> delSuccess = new MutableLiveData<>();
    public MutableLiveData<SalaryStandardEntity> salaryStandard = new MutableLiveData<>();
    public MutableLiveData<Boolean> delSalaryStandardSuccess = new MutableLiveData<>();
    public MutableLiveData<Boolean> addSalaryStandard = new MutableLiveData<>();

    public PointWorkEditFragViewModel(@NonNull @NotNull Application application) {
        super(application);
    }

    public void uploadFile(List<File> files) {
        iView.loading();
        Map<String, RequestBody> photos = new HashMap<>();
        int i = 0;
        for (File file : files) {
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
            photos.put("files" + i + "\"; filename=\""  + i + "image.jpg\"" , requestBody);
            i++;
        }
        CommonNet.getInstance().getService().uploadMutiFileDrive(photos).enqueue(new ICallBack<BaseEntity<List<String>>>() {
            @Override
            public void onSuccess(BaseEntity<List<String>> data) {
                if (data.isSuccess()) {
                    uploadUrls.postValue(data.getData());
                }
            }
        });
    }

    public void uploadWork(Map<String, Object> map) {
        iView.loading();
        PackersNet.getInstance().getService().uploadWork(map).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                uploadSuccess.postValue(true);
            }
        });
    }

    public void getPointDetail(String no,String type) {
        iView.loading();
        PackersNet.getInstance().getService().pointWorkDetail(no,type).enqueue(new ICallBack<BaseEntity<WorkRecordsEntity>>() {
            @Override
            public void onSuccess(BaseEntity<WorkRecordsEntity> data) {
                pointDetail.postValue(data.getData());
            }
        });
    }

    public void delPoint(String no) {
        iView.loading();
        PackersNet.getInstance().getService().delPoint(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                delSuccess.postValue(true);
            }
        });
    }

    public void salaryStandard(String no) {
        iView.loading();
        PackersNet.getInstance().getService().salaryStandard(no,"1").enqueue(new ICallBack<BaseEntity<SalaryStandardEntity>>() {
            @Override
            public void onSuccess(BaseEntity<SalaryStandardEntity> data) {
                salaryStandard.postValue(data.getData());
            }
        });
    }

    public void addSalaryStandard(Map<String,Object> map) {
        iView.loading();
        PackersNet.getInstance().getService().addSalaryStandard(map).enqueue(new ICallBack<BaseEntity<BaseEntity>>() {
            @Override
            public void onSuccess(BaseEntity<BaseEntity> data) {
                addSalaryStandard.postValue(true);
            }
        });
    }

    public void delSalaryStandard(String no) {
        iView.loading();
        PackersNet.getInstance().getService().delSalaryStandard(no).enqueue(new ICallBack<BaseEntity>() {
            @Override
            public void onSuccess(BaseEntity data) {
                delSalaryStandardSuccess.postValue(true);
            }
        });
    }
}
